﻿using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ZhongKeSite.Common;
using ZhongKeSite.Common.Helper;
using ZhongKeSite.Entity;
using ZhongKeSite.Entity.Models;
using ZhongKeSite.Repository;

namespace ZhongKeSite.Service
{
    public class BasisService : IBasisService
    {
        ICategoryRepository categoryRepository;
        public BasisService(ICategoryRepository _categoryRepository)
        {
            categoryRepository = _categoryRepository;
        }
        #region Category
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LayPager<Category> GetCategoryPagedList(int pageIndex, int pageSize, Expression<Func<Category, bool>> predicate)
        {
            if (predicate == null)
            {
                predicate = k => k.IsDel == 0;
            }
            var list = categoryRepository.GetPagedList(pageIndex, pageSize, predicate);
            LayPager<Category> pmodel = new LayPager<Category>();
            pmodel.count = list.TotalItemCount;
            pmodel.msg = CommonConst.Success;
            pmodel.data = list.ToList();
            return pmodel;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultResponse<Category> GetCategory(string id)
        {
            ResultResponse<Category> resultResponse = new ResultResponse<Category>() { success = false, msg = CommonConst.Fail };
            resultResponse.data = categoryRepository.GetById(id);
            if (resultResponse.data != null)
            {
                resultResponse.success = true;
                resultResponse.msg = CommonConst.Success;
            }
            return resultResponse;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse AddCategory(Category model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.ID))
            {
                response.msg = "分类编码不能为空!";
                return response;
            }
            Category category = categoryRepository.GetById(model.ID);
            if (category != null)
            {
                response.msg = "分类编码已经存在!";
                return response;
            }
            if (string.IsNullOrWhiteSpace(model.CategoryName))
            {
                response.msg = "分类名称不能为空!";
                return response;
            }
            model.IsDel = 0;
            model.AddDate = DateTime.Now;
            response.success = categoryRepository.Add(model);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse UpdateCategory(Category model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.ID))
            {
                response.msg = "分类编码不能为空!";
                return response;
            }
            if (string.IsNullOrWhiteSpace(model.CategoryName))
            {
                response.msg = "分类名称不能为空!";
                return response;
            }
            var o = categoryRepository.GetById(model.ID);
            o.CategoryName = model.CategoryName;
            o.IconImage = model.IconImage;
            o.CategoryType = model.CategoryType;
            o.IsAvailable = model.IsAvailable;
            o.SortNo = model.SortNo;
            o.UpdateBy = model.UpdateBy;
            o.UpdateDate = DateTime.Now;

            response.success = categoryRepository.Update(o);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public ResultResponse DeleteCategory(string id)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            response.success = categoryRepository.Delete(s => s.ID == id);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 获取当前列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SelectListItem> GetCategoryListDLL(string categoryType,bool isSelect = false,bool isAll = false)
        {
            List<SelectListItem> itemlist = new List<SelectListItem>();
            IEnumerable<Category> list = categoryRepository.GetList(k => k.CategoryType == categoryType && k.IsAvailable == 1 && k.IsDel == 0);
            if (list != null && list.Any())
            {
                itemlist = list.Select(p => new SelectListItem()
                {
                    Value = p.ID,
                    Text = p.CategoryName
                }).ToList();
            }
            if(isSelect)
            {
                itemlist.Insert(0, new SelectListItem() { Value = string.Empty,Text = "--请选择--" });
            }
            if(isAll)
            {
                itemlist.Insert(0, new SelectListItem() { Value = string.Empty, Text = "--全部--" });
            }
            return itemlist;
        }
        #endregion
    }
}
